package com.weiwei.diancan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.http.RetrofitFactory;
import com.weiwei.diancan.view.EmptyLayout;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.adapter.BaseQuickAdapter;
import weiwei.com.weiweilibrary.holder.BaseViewHolder;
import weiwei.com.weiweilibrary.views.ToastUtil;

/**
 * Created by BMW on 2016/11/23.
 */
public class OrderDetailAdapter extends BaseQuickAdapter<LoginBean.OrderListBean> {
    private Context context;
    private SwipeMenuCreator swipeMenuCreator;
    private OnItemClickListener listener;

    public OrderDetailAdapter(List<LoginBean.OrderListBean> data, Context context) {
        super(R.layout.item_order_detail, data);
        this.context = context;
        int width = context.getResources().getDimensionPixelSize(R.dimen.di);
        swipeMenuCreator= (swipeLeftMenu, swipeRightMenu, viewType) -> {
            SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
                    .setBackgroundDrawable(R.color.my_titler)
                    .setText("删除") // 文字。
                    .setTextColor(Color.WHITE) // 文字颜色。
                    .setTextSize(16).setWidth(width).setHeight(ViewGroup.LayoutParams.MATCH_PARENT); // 文字大小。

            swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。.
        };
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, LoginBean.OrderListBean item, int position) {



        LinearLayout ll_item = (LinearLayout) helper.getView(R.id.ll_item);

        ll_item.setOnClickListener(v->{
            if(listener!=null) {
                listener.onItemClick(ll_item, position);
            }
        });
        TextView tv_time = helper.getView(R.id.tv_time);
        TextView tv_state = helper.getView(R.id.tv_state);
        TextView tv_price = helper.getView(R.id.tv_price);
        if(item.getOrder_state().equals("1")){  //未结账订单
            ll_item.setBackgroundResource(R.color.white);
            tv_time.setTextColor(Color.parseColor("#B41B1B"));
            tv_state.setText("结账");
            tv_state.setTextColor(Color.parseColor("#B41B1B"));
            tv_price.setTextColor(Color.parseColor("#B41B1B"));
            tv_state.setOnClickListener(v -> {
                if(listener!=null) {
                    listener.onJiezhang(item.getOrder_id(), item,tv_state);
                }
            });
        }else if(item.getOrder_state().equals("2")){ //已结账订单
            ll_item.setBackgroundResource(R.color.e9e9e9);
            tv_time.setTextColor(Color.parseColor("#777777"));
            tv_state.setText("已结账");
            tv_state.setTextColor(Color.parseColor("#777777"));
            tv_price.setTextColor(Color.parseColor("#777777"));
        }

        double price = 0f;
        for(LoginBean.OrderListBean.LogListBean logListBean : item.getLog_list()){
            price+=Double.parseDouble(logListBean.getLog_money());
        }
        tv_price.setText("小计：¥"+price);


        if(item.getOrder_type().equals("0")){   ///主订单
            tv_time.setText("主订单："+item.getOrder_no()+"("+item.getInsert_time()+")");

        }else if(item.getOrder_type().equals("1")){   //追加订单
            tv_time.setText("追加订单："+item.getOrder_no()+"("+item.getInsert_time()+")");
        }


        SwipeMenuRecyclerView recycler = (SwipeMenuRecyclerView) helper.getView(R.id.recycler);
        recycler.setSwipeMenuCreator(swipeMenuCreator);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.setItemAnimator(new DefaultItemAnimator());
        MenuAdapter menuAdapter = new MenuAdapter(item.getLog_list(),position,item.getOrder_state());
        /**
         * 删除
         */
        recycler.setSwipeMenuItemClickListener((closeable, adapterPosition, menuPosition, direction) -> {
//            l.remove(adapterPosition);
//            menuAdapter.notifyDataSetChanged();
//            recycler.smoothCloseMenu();

            String order_id = item.getOrder_id();
            String log_id = item.getLog_list().get(adapterPosition).getLog_id();
            if(listener!=null) {
                if(item.getOrder_state().equals("2")) {
                    ToastUtil.showToast(context,"已结账订餐品无法删除！");
                }else{
                    listener.onShanchu(order_id, log_id,menuAdapter);
                    recycler.smoothCloseMenu();
                }
            }

        });
        recycler.setAdapter(menuAdapter);

    }

    public interface OnItemClickListener{
        void onItemClick(View v,int pos);
        void onShanchu(String orderId,String logId,MenuAdapter menuAdapter);
        void onJiezhang(String orderId,LoginBean.OrderListBean item,TextView textView);
    }

    public class MenuAdapter extends SwipeMenuAdapter<MenuAdapter.MyVH>{

        private List<LoginBean.OrderListBean.LogListBean> data;
        private int pos;
        private String state;

        public MenuAdapter(List<LoginBean.OrderListBean.LogListBean> data,int pos,String state){
            this.data = data;
            this.pos = pos;
            this.state = state;
        }

        @Override
        public View onCreateContentView(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_swip, parent, false);
            return inflate;
        }

        @Override
        public MyVH onCompatCreateViewHolder(View realContentView, int viewType) {
            return new MyVH(realContentView);
        }

        @Override
        public void onBindViewHolder(MyVH holder, int position) {
            LoginBean.OrderListBean.LogListBean logListBean = data.get(position);
            if(state.equals("1")){  //未结账订单
                holder.ll_all.setBackgroundResource(R.color.white);
                holder.iv_tao.setImageResource(R.mipmap.taocan_red);
            }else if(state.equals("2")){ //已结账订单
                holder.ll_all.setBackgroundResource(R.color.e9e9e9);
                holder.iv_tao.setImageResource(R.mipmap.taocan_gray);
            }

            if(logListBean.getLog_type().equals("1")){
                holder.iv_tao.setVisibility(View.VISIBLE);
            }else{
                holder.iv_tao.setVisibility(View.GONE);
            }
            holder.tv_name.setText(logListBean.getLog_name());
            holder.tv_num.setText(logListBean.getLog_price());
            holder.tv_shu.setText(logListBean.getLog_count());
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class MyVH extends RecyclerView.ViewHolder{
            private final RelativeLayout ll_all;
            private final TextView tv_name;
            private final TextView tv_num;
            private final TextView tv_x;
            private final TextView tv_shu;
            private final ImageView iv_tao;

            public MyVH(View itemView) {
                super(itemView);
                tv_name = (TextView)itemView.findViewById(R.id.tv_name);
                iv_tao = (ImageView)itemView.findViewById(R.id.iv_tao);
                tv_num = (TextView)itemView.findViewById(R.id.tv_num);
                tv_x = (TextView)itemView.findViewById(R.id.tv_x);
                tv_shu = (TextView)itemView.findViewById(R.id.tv_shu);
                ll_all = (RelativeLayout)itemView.findViewById(R.id.ll_all);
                itemView.setOnClickListener(v -> {
                    listener.onItemClick(itemView,pos);
                });
            }
        }
    }
}
